package com.efounder.frame.title;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.efounder.mobilemanager.R;
import com.efounder.view.StatusPlaceholderView;

/**
 * Created by hudq on 2016/8/16.
 */

public abstract class EFTitleView extends FrameLayout {
    /**titleView高度，单位：dp**/
//    private static final int EF_TITLE_VIEW_HEIGHT = 50;
//    private static final int EF_TITLE_LEFT_LAYOUT_WIDTH = 60;
//    private static final int EF_TITLE_RIGHT_LAYOUT_WIDTH = 60;
    private LinearLayout containerLayout;
    private LinearLayout titleContainerLayout;
    private LinearLayout leftLayout;
    private LinearLayout centerLayout;
    private LinearLayout rightLayout;
    private LinearLayout secondLayout;
    private ProgressBar progressBar;
    private boolean isRightLayoutHiddenAtFirst = false;

    public EFTitleView(Context context) {
        super(context);
        //设置EFTitleView宽高
        ViewGroup.LayoutParams thisParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setLayoutParams(thisParams);
        //LinearLayout
        containerLayout = new LinearLayout(getContext());
        FrameLayout.LayoutParams containerLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) context.getResources().getDimension(R.dimen.ef_title_view_height));
        containerLayout.setLayoutParams(containerLayoutParams);
        containerLayout.setOrientation(LinearLayout.HORIZONTAL);//垂直布局
        FrameLayout.LayoutParams titleContainerLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //垂直布局
        titleContainerLayout = new LinearLayout(getContext());
        titleContainerLayout.setLayoutParams(titleContainerLayoutParams);
        titleContainerLayout.setOrientation(LinearLayout.VERTICAL);
        //添加占位布局  沉浸式状态栏 时显示
        StatusPlaceholderView placeholderView = new StatusPlaceholderView(getContext());
        titleContainerLayout.addView(placeholderView);
        titleContainerLayout.addView(containerLayout);
        this.addView(titleContainerLayout);

        initLayout(context);
    }

    private void initLayout(Context context){
        //1.创建leftLayout
        leftLayout = new LinearLayout(context);
        LinearLayout.LayoutParams leftLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        leftLayout.setLayoutParams(leftLayoutParams);
        leftLayout.setGravity(Gravity.CENTER_VERTICAL);//垂直居中
        containerLayout.addView(leftLayout);
        initLeftLayout(leftLayout);

        //2.创建centerLayout
        centerLayout = new LinearLayout(context);
        LinearLayout.LayoutParams centerLayoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
        centerLayoutParams.weight = 1;
        centerLayout.setLayoutParams(centerLayoutParams);
        centerLayout.setGravity(Gravity.CENTER_VERTICAL);//垂直居中
        containerLayout.addView(centerLayout);
        initCenterLayout(centerLayout);

        //3.创建rightLayout
        rightLayout = new LinearLayout(context);
        LinearLayout.LayoutParams rightLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        rightLayout.setLayoutParams(rightLayoutParams);
        rightLayout.setGravity(Gravity.CENTER_VERTICAL);//垂直居中
        containerLayout.addView(rightLayout);
        initRightLayout(rightLayout);

        //4.创建secondLayout
        secondLayout = new LinearLayout(context);
        LinearLayout.LayoutParams secondLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        secondLayout.setLayoutParams(secondLayoutParams);
        secondLayout.setOrientation(LinearLayout.HORIZONTAL);
//        secondLayout.setBackgroundColor(getResources().getColor(R.color.ef_transparent_black));
        secondLayout.setVisibility(View.GONE);
        this.addView(secondLayout);

        //5.创建progressbar
        progressBar = new ProgressBar(context);
        LinearLayout.LayoutParams pbParams = new LinearLayout.LayoutParams((int) context.getResources().getDimension(R.dimen.ef_title_progress_bar_size), (int)context.getResources().getDimension(R.dimen.ef_title_progress_bar_size));
        pbParams.gravity = Gravity.CENTER;
        pbParams.setMargins(35,20,30,15);
        progressBar.setLayoutParams(pbParams);
//        progressBar.setBackgroundColor(getResources().getColor(R.color.ef_transparent_gray_deeper));
        progressBar.setIndeterminateDrawable(getResources().getDrawable(R.drawable.progress_circle));
        secondLayout.addView(progressBar);
    }

    public LinearLayout getLeftLayout() {
        return leftLayout;
    }

    public LinearLayout getCenterLayout() {
        return centerLayout;
    }

    public LinearLayout getRightLayout() {
        return rightLayout;
    }


    public abstract void initLeftLayout(LinearLayout leftLayout);
    public abstract void initCenterLayout(LinearLayout centerLayout);
    public abstract void initRightLayout(LinearLayout rightLayout);

    //显示左侧ProgressBar
    public LinearLayout showLeftProgressBar(){
        leftLayout.setVisibility(View.INVISIBLE);
        secondLayout.setVisibility(View.VISIBLE);
        secondLayout.setGravity(Gravity.START);
        return secondLayout;
    }
    //显示右侧ProgressBar
    public LinearLayout showRightProgressBar(){
        if (rightLayout.getVisibility()!=View.VISIBLE){
            secondLayout.setVisibility(View.VISIBLE);
            secondLayout.setGravity(Gravity.END);
            isRightLayoutHiddenAtFirst = true;
        }else {
            rightLayout.setVisibility(View.INVISIBLE);
            secondLayout.setVisibility(View.VISIBLE);
            secondLayout.setGravity(Gravity.END);
            isRightLayoutHiddenAtFirst = false;
        }
        return secondLayout;
    }
    //取消显示左侧ProgressBar
    public void dismissLeftProgressBar(){
        leftLayout.setVisibility(View.VISIBLE);
        secondLayout.setVisibility(View.GONE);
    }
    //取消显示右侧ProgressBar
    public void dismissRightProgressBar(){
        if (isRightLayoutHiddenAtFirst){
            secondLayout.setVisibility(View.GONE);
        }else {
            rightLayout.setVisibility(View.VISIBLE);
            secondLayout.setVisibility(View.GONE);
        }

    }
}
